import { reactive, onMounted, ref, nextTick } from 'vue'
import { StageTable, AddModel } from '@/api/01work/01type'
import { userPageApi ,userSaveApi,userUpdateApi} from '@/api/user/user'
import { UserPage, UserTable,UserAddModel } from '@/api/user/UserModel'
import { Result, StatusCode } from '@/utils/request';
import { EditType } from '@/type/BaseEnum'
import useInstance from '@/hooks/useInstance';
export default function getWorkList() {
  const RefUserAddEdit = ref<{ show: (type: string, row?: AddModel) => void }>();
  const { global, proxy } = useInstance();
  // table数据
  const tableData = reactive({
    list: [],
    Loading: false,
    tableHeigth: 0,
    total: 0
  })
  // 查询参数
  let searchParams = reactive<UserPage>({
    page: 1,
    size: 10,
    name: '',
    gender: '',
    state: '',
    username: ''
  })
  const getTableList = async () => {
    tableData.Loading = true
    const res = await userPageApi(searchParams)
    if (res.code === 200) {
      tableData.Loading = false
      tableData.list = res.data.records
      tableData.total = res.data.total
    }
  }
  // 查询调用
  onMounted(() => {
    getTableList()
    nextTick(() => {
      tableData.tableHeigth = window.innerHeight - 185
    })
  })

  const editBtn = (row: AddModel) => {
    RefUserAddEdit.value?.show(EditType.EDIT, row);
  }
  const addBtn = () => {
    RefUserAddEdit.value?.show(EditType.ADD);
  }
  const save = async (params: UserAddModel) => {
    console.log(params, 'params save');
    let res
    if (params?.id) {
      res = await userUpdateApi(params)
    } else {
      res = await userSaveApi(params)
    }
    //  
    if (res.code === 200 && params?.id) {
      global.$message({ message: '编辑成功', type: 'success' })
      getTableList();
    } else {
      global.$message({ message: '新增成功', type: 'success' })
      getTableList();
    }
  }
  const resetBtn = async () => {
    searchParams = {
      page: 1,
      size: 10,
      name: '',
      gender: '',
      state: '',
      username: ''
    }
    getTableList()
  }
  const tableRowClassName = ({ row, rowIndex }) => {
    if (rowIndex % 2 === 1) {
      return "warning-row";
    } else {
      return "success-row";
    }
  }
  const handleCurrentChange = (current) => {
    searchParams.page = current
    getTableList()
  }
  const handleSizeChange = (size) => {
    searchParams.size = size
    getTableList()
  }
  return {
    getTableList, tableData, searchParams, RefUserAddEdit, addBtn, save, editBtn, resetBtn,
    handleCurrentChange,handleSizeChange,tableRowClassName
  }
}